package com.ourteam.workspace.api;

import org.apache.commons.lang3.ArrayUtils;

import com.ourteam.product.ui.dao.ProductUIPageDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIPageBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.UIConfigFactory;
import net.ui.model.action.ActionGroupBean;
import net.ui.model.constlist.ListDataContainer;
import net.ui.model.form.FormBean;
import net.ui.model.panel.PanelBean;
import net.ui.model.tab.TabbedPaneConfig;
import net.ui.model.table.TableBean;

public class UIConfgiUtils {
	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	protected UIConfigFactory getUIConfigFactory(String id) {
		return UIConfigFactory.getInstance(id);
	}

	public FormBean form(String formId) throws Exception {

		return getUIConfigFactory(formId).getFormConfig(formId);
	}

	public TableBean table(String tableId) throws Exception {
		return getUIConfigFactory(tableId).getTableConfig(tableId);
	}

	public PanelBean panel(String panelId) throws Exception {
		return getUIConfigFactory(panelId).getPanelConfig(panelId);
	}

	public ActionGroupBean actionGroup(String actionGroupId) throws Exception {
		return getUIConfigFactory(actionGroupId).getActionGroupConfig(actionGroupId);
	}

	public ListDataContainer list(String listId) throws Exception {
		return getUIConfigFactory(listId).getListDataConfig(listId);
	}

	public TabbedPaneConfig tab(String tabId) throws Exception {
		return getUIConfigFactory(tabId).getTabConfig(tabId);
	}

	public ProductUIPageBean page(String pageId) throws Exception {

		ProductUIPageDAOQueryBean queryBean = new ProductUIPageDAOQueryBean();

		queryBean.createCriteria().andPageCodeEqualTo(pageId);

		ProductUIPageBean[] pageBeans = PRODUCT_UI_SERVICE.queryProductUIPages(queryBean);

		if (ArrayUtils.isNotEmpty(pageBeans)) {
			return pageBeans[0];
		} else {
			return null;
		}
	}

	public String jsonConfigData() throws Exception {
		return UIConfigFactory.getJSONConfigData();
	}
}
